package com.kgc.service.impl;

import com.kgc.dao.OrderMapper;
import com.kgc.dao.OrderMapperEx;
import com.kgc.pojo.Order;
import com.kgc.pojo.OrderEx;
import com.kgc.pojo.OrderExample;
import com.kgc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderMapperEx orderMapperEx;
    @Cacheable(value = "order",key = "'getOrderCount'+#userId")
    @Override
    public int getOrderCount(Integer userId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if(userId != null ){
            criteria.andUseridEqualTo(userId);
        }
        return orderMapper.countByExample(orderExample);
    }

    //@Cacheable(value = "order",key = "'getOrderInfo'+#pageNow+','+#pageSize+','+#userId")
    @Override
    public List<OrderEx> getOrderInfo(Integer pageNow, Integer pageSize, Integer userId) {
        Integer start = (pageNow-1)*pageSize;
        return orderMapperEx.getOrderInfo(start,pageSize,userId);
    }

    @Cacheable(value = "order",key = "'getOrderDetailInfo'+#userId+','+#orderId")
    @Override
    public List<OrderEx> getOrderDetailInfo(Integer userId, Integer orderId) {
        return orderMapperEx.getOrderDetailInfo(userId,orderId);
    }

    @CacheEvict(value = "order",allEntries = true)
    @Override
    public int delOrderById(Integer orderId) {
        return orderMapper.deleteByPrimaryKey(orderId);
    }

    @Cacheable(value = "order",key = "'getOrderById'+#orderId")
    @Override
    public Order getOrderById(Integer orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    @Cacheable(value = "order",key = "'getOrderList'+#orderStatusId")
    public List<OrderEx> getOrderList(Integer orderStatusId) {

        return orderMapperEx.getOrderList(orderStatusId);
    }

    @Override
    @CacheEvict(value = "order",allEntries = true)
    public int changeOrderStatus(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        order.setStatusid(3);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

}
